package com.lingmeng.system.controller;

import com.lingmeng.common.core.controller.BaseController;
import com.lingmeng.common.core.entity.AjaxResult;
import com.lingmeng.common.core.page.TableDataInfo;
import com.lingmeng.common.core.utils.poi.ExcelUtil;
import com.lingmeng.common.log.annotation.Log;
import com.lingmeng.common.log.enums.BusinessType;
import com.lingmeng.common.security.annotation.RequiresPermissions;
import com.lingmeng.common.security.utils.SecurityUtils;
import com.lingmeng.system.entity.Equipment;
import com.lingmeng.system.entity.excel.EquipExcel;
import com.lingmeng.system.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备Controller
 *
 * @author lingmeng
 * @date 2024-07-18
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController extends BaseController {
	@Autowired
	private IEquipmentService equipmentService;


	/**
	 * 查询设备列表
	 */
	@RequiresPermissions("business:equipment:list")
	@GetMapping("/list")
	public TableDataInfo list(Equipment equipment) {
		startPage();
		List<Equipment> list = equipmentService.selectEquipmentList(equipment);
		return getDataTable(list);
	}

	@Log(title = "设备管理", businessType = BusinessType.IMPORT)
	@RequiresPermissions("business:equipment:import")
	@PostMapping("/importData")
	public AjaxResult importData(MultipartFile file) throws Exception {
		ExcelUtil<Equipment> util = new ExcelUtil<>(Equipment.class);
		List<Equipment> list = util.importExcel(file.getInputStream());
		String operName = SecurityUtils.getUsername();
		String message = equipmentService.importEquip(list, operName);
		return success(message);
	}

	@PostMapping("/importTemplate")
	public void importTemplate(HttpServletResponse response) {
		ExcelUtil<EquipExcel> util = new ExcelUtil<>(EquipExcel.class);
		util.importTemplateExcel(response, "设备数据");
	}

	/**
	 * 导出设备列表
	 */
	@RequiresPermissions("business:equipment:export")
	@Log(title = "设备", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, Equipment equipment) {
		List<Equipment> list = equipmentService.selectEquipmentList(equipment);
		ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
		util.exportExcel(response, list, "设备数据");
	}

	/**
	 * 获取设备详细信息
	 */
	@RequiresPermissions("business:equipment:query")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return success(equipmentService.selectEquipmentById(id));
	}

	/**
	 * 新增设备
	 */
	@RequiresPermissions("business:equipment:add")
	@Log(title = "设备", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Equipment equipment) {
		return toAjax(equipmentService.insertEquipment(equipment));
	}

	/**
	 * 修改设备
	 */
	@RequiresPermissions("business:equipment:edit")
	@Log(title = "设备", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Equipment equipment) {
		return toAjax(equipmentService.updateEquipment(equipment));
	}

	/**
	 * 删除设备
	 */
	@RequiresPermissions("business:equipment:remove")
	@Log(title = "设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(equipmentService.deleteEquipmentByIds(ids));
	}

	/**
	 * 设备开机
	 */
	@RequiresPermissions("business:equipment:power")
	@Log(title = "设备", businessType = BusinessType.UPDATE)
	@GetMapping("/on/{id}")
	public AjaxResult on(@PathVariable("id") Long id) {
		equipmentService.on(id);
		return AjaxResult.success();
	}

	/**
	 * 设备关机
	 */
	@RequiresPermissions("business:equipment:power")
	@Log(title = "设备", businessType = BusinessType.UPDATE)
	@GetMapping("/off/{id}")
	public AjaxResult off(@PathVariable("id") Long id) {
		equipmentService.off(id);
		return AjaxResult.success();
	}

	/**
	 * 设备激活
	 *
	 * @param id id
	 * @return AjaxResult
	 */
	@RequiresPermissions("business:equipment:activation")
	@Log(title = "设备", businessType = BusinessType.UPDATE)
	@GetMapping("/activation/{id}/{userId}")
	public AjaxResult activation(@PathVariable Long id, @PathVariable Long userId) {
		equipmentService.activation(id, userId);
		return AjaxResult.success();
	}
}
